package com.cf.githubuseractivity;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.*;

/**
 * Copyright(C) 2025- com.cf
 * FileName:    App
 * Author:      cf
 * Date:        2025/1/9 21:50
 * Description:
 */
public class App {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String input = scanner.nextLine();
        if (!input.startsWith("github-activity")) {
            System.err.println("Usage: github-activity <username>");
            System.exit(1);
        }

        String username = input.replace("github-activity", "").trim();

        if (username.isEmpty()) {
            System.err.println("Invalid username");
            return;
        }

        String githubApi = "https://api.github.com/users/" + username + "/events";

        try {
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(new URI(githubApi))
                    .GET()
                    .build();

            HttpResponse<String> response =
                    client.send(request, HttpResponse.BodyHandlers.ofString());

            System.out.println("Output:");

            if (response.statusCode() == 200) {
                JSONArray array = JSONArray.parseArray(response.body());

                if (array.isEmpty()) {
                    System.out.println("No events found");
                }

                for (int i = 0; i < array.size(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    GithubEvent githubEvent = JSONObject.parseObject(obj.toString(), GithubEvent.class);
                    String type = githubEvent.getType();
                    Repo repo = githubEvent.getRepo();
                    JSONObject payload = obj.getJSONObject("payload");
                    String action = (String) payload.get("action");
                    switch (type) {
                        case "CommitCommentEvent":
                        case "IssueCommentEvent":
                            System.out.println("- " + action.substring(0, 1).toUpperCase()
                                    + action.substring(1) + " a "
                                    + type.replace("CommentEvent", "").toLowerCase()
                                    + " comment to " + repo.getName());
                            break;
                        case "CreateEvent":
                            String refType = (String) payload.get("ref_type");
                            if ("repository".equals(refType)) {
                                System.out.println("- " + type.replace("Event", "") + "d a "
                                        + refType + " named " + repo.getName());
                            } else {
                                System.out.println("- " + type.replace("Event", "") + "d a "
                                        + refType + " to " + repo.getName());
                            }
                            break;
                        case "DeleteEvent":
                            String refTypeDel = (String) payload.get("ref_type");
                            System.out.println("- " + type.replace("Event", "") + "d a "
                                    + refTypeDel + " to " + repo.getName());
                            break;
                        case "ForkEvent":
                            System.out.println("- " + type.replace("Event","") + "ed " + repo.getName());
                            break;
                        case "GollumEvent":
                            JSONArray pagesObj = payload.getJSONArray("pages");
                            System.out.println("- Updated wiki " + pagesObj.size() + " page(s) to " + repo.getName());
                            break;
                        case "IssuesEvent":
                            System.out.println("- " + action.substring(0, 1).toUpperCase()
                                    + action.substring(1) + " a issue to " + repo.getName());
                            break;
                        case "MemberEvent":
                            System.out.println("- " + action.substring(0, 1).toUpperCase()
                                    + action.substring(1) + " to be a member of " + repo.getName());
                            break;
                        case "PublicEvent":
                            System.out.println("- Maked " + repo.getName() + " public");
                            break;
                        case "PullRequestEvent":
                            Integer number = (Integer) payload.get("number");
                            System.out.println("- " + action.substring(0, 1).toUpperCase()
                                    + action.substring(1) + " "
                                    + number + " pull_request to " + repo.getName());
                            break;
                        case "PullRequestReviewEvent":
                        case "PullRequestReviewCommentEvent":
                        case "PullRequestReviewThreadEvent":
                        case "ReleaseEvent":
                        case "SponsorshipEvent":
                            System.out.println("- " + action.substring(0, 1).toUpperCase()
                                    + action.substring(1) + " a "
                                    + type.replace("Event", "").toLowerCase()
                                    + " to " + repo.getName());
                            break;
                        case "PushEvent":
                            Integer size = (Integer) payload.get("size");
                            System.out.println("- Pushed " + size + " commit(s) to " + repo.getName());
                            break;
                        case "WatchEvent":
                            System.out.println("- " + action.substring(0, 1).toUpperCase()
                                    + action.substring(1) + " " + repo.getName());
                    }
                }
            } else {
                JSONObject jsonObject = JSONObject.parseObject(response.body());
                System.err.println(jsonObject.get("message"));
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
